
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>自定义验证码 | home</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="">
    <meta name="description" content="自定义验证码1234567891011121314151617181920212223242526272829303132333435363738394041424344454647#自定义图片验证码class MyCode(View):	#定义rgb随机颜色	def get_random_color(self):		R &amp;#x3D; random.randrange(255)		G &amp;#x3D; random.randrange(255)		B &amp;#x3D; random.randrange(255)		return (R,G,B)	#定义图片视图	def get(self,request):		#画布		img_si...">
    
        <link rel="icon" href="/favicon.ico">
    
    
        
            <link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
        
            <link rel="stylesheet" href="/css/stage.css">
        
            <link rel="stylesheet" href="/css/avatar-bg.css">
        
    
<meta name="generator" content="Hexo 4.2.0"></head>

<body>
<header id="header">
    <div class="menu">
        <i class="fa fa-bars"></i>
    </div>
    <div class="header-main">
        <h1><a href="/">home</a></h1>
    </div>
    <div id="nav">
        <div class="nav-img" id="nav-img"></div>
        <div class="sentences">
            路遥知马力，日久见人心。——《元曲选·争报恩》
        </div>
    </div>
</header>

<div id="content-outer">
    <div id="content-inner">
        <div class="clearfix">
    <article id="post">
        <h1>自定义验证码</h1>
        <div class="create">
            <span>Created</span>
            
                <time datetime="2020-04-25T18:59:06.000Z">
                    2020-04-26
                </time>
            
            
        </div>
        <h1 id="自定义验证码"><a href="#自定义验证码" class="headerlink" title="自定义验证码"></a>自定义验证码</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">#自定义图片验证码</span><br><span class="line">class MyCode(View):</span><br><span class="line"></span><br><span class="line">	#定义rgb随机颜色</span><br><span class="line">	def get_random_color(self):</span><br><span class="line"></span><br><span class="line">		R &#x3D; random.randrange(255)</span><br><span class="line">		G &#x3D; random.randrange(255)</span><br><span class="line">		B &#x3D; random.randrange(255)</span><br><span class="line"></span><br><span class="line">		return (R,G,B)</span><br><span class="line"></span><br><span class="line">	#定义图片视图</span><br><span class="line">	def get(self,request):</span><br><span class="line">		#画布</span><br><span class="line">		img_size &#x3D; (120,50)</span><br><span class="line">		#定义图片对象</span><br><span class="line">		image &#x3D; Image.new(&#39;RGB&#39;,img_size,&#39;white&#39;)</span><br><span class="line">		#定义画笔</span><br><span class="line">		draw &#x3D; ImageDraw.Draw(image,&#39;RGB&#39;)</span><br><span class="line">		source &#x3D; &#39;0123456789abcdefghijk&#39;</span><br><span class="line">		#接收容器</span><br><span class="line">		code_str &#x3D; &#39;&#39;</span><br><span class="line">		#进入循环绘制</span><br><span class="line">		for i in range(4):</span><br><span class="line">			#获取字母颜色</span><br><span class="line">			text_color &#x3D; self.get_random_color()</span><br><span class="line">			#获取随机下标</span><br><span class="line">			tmp_num &#x3D; random.randrange(len(source))</span><br><span class="line">			#随机字符串</span><br><span class="line">			random_str &#x3D; source[tmp_num]</span><br><span class="line">			#装入容器</span><br><span class="line">			code_str +&#x3D; random_str</span><br><span class="line">			#绘制字符串</span><br><span class="line">			draw.text((10+30*i,20),random_str,text_color)</span><br><span class="line">		#获取缓存区</span><br><span class="line">		buf &#x3D; io.BytesIO()</span><br><span class="line">		#将临时图片保存到缓冲</span><br><span class="line">		image.save(buf,&#39;png&#39;)</span><br><span class="line">		#保存随机码</span><br><span class="line">		r.set(&#39;code&#39;,code_str)</span><br><span class="line">		#保存session</span><br><span class="line">		request.session[&#39;code&#39;] &#x3D; code_str</span><br><span class="line"></span><br><span class="line">		print(r.get(&#39;code&#39;))</span><br><span class="line"></span><br><span class="line">		return HttpResponse(buf.getvalue(),&#39;image&#x2F;png&#39;)</span><br></pre></td></tr></table></figure>


<p><img src="/hugo_blog/0d8a90abc2d78eaa5e01d3fb7f57e6da.jpg" alt="每日一图"></p>

        <div>
            <ul class="tags-category-list" itemprop="keywords"><li class="tags-category-list-item"><a class="tags-category-list-link" href="/hexo_blog/tags/python/" rel="tag">python</a></li></ul>
        </div>
        <div class="bottom-line"></div>
        
    <nav id="article-nav">
        
            <a href="/hexo_blog/2020/04/28/%E7%AC%AC%E4%B8%89%E6%96%B9%E7%99%BB%E9%99%86/" id="article-nav-newer" class="article-nav-link-wrap">
        <span class="article-nav-title">
            
                第三方登陆
            
        </span>
                <strong class="article-nav-caption">&gt;</strong>
            </a>
        
        
            <a href="/hexo_blog/2020/04/25/%E6%BB%91%E5%8A%A8%E9%AA%8C%E8%AF%81/" id="article-nav-older" class="article-nav-link-wrap">
                <strong class="article-nav-caption">&lt;</strong>
                <span class="article-nav-title">
                    
                        滑动验证
                </span>
                
            </a>
        
    </nav>


        
    </article>
    <div id="toc">
        
            <h2>Table of Contents</h2>
            <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#自定义验证码"><span class="toc-number">1.</span> <span class="toc-text">自定义验证码</span></a></li></ol>
        
    </div>
</div>

    </div>
</div>
<footer id="footer">
    <div id="copyright">&copy; Mr.Song  2020</div>
    <div id="theme">
        Powered by <a href="http://hexo.io" target="_blank" rel="noopener">Hexo</a>. Theme by <a href="https://github.com/markyong/hexo-theme-stage" target="_blank" rel="noopener">Stage</a>
    </div>
</footer>
<script src="/lib/js/waterrippleeffect.min.js"></script>
<script src="/js/header-bg.main.js"></script>

    <script src="/lib/js/smooth-scroll.min.js"></script>
    <script src="/js/toc.main.js"></script>

</body>
</html>
